package com.huiyi.quickproj.jundui.utils;

import org.apache.commons.io.input.BOMInputStream;

import java.io.*;

/**
 * describe:
 *
 * @author: robin
 * @email:  cuizhou6463@126.com
 * @date 2023-12-09
 */
public class FileEncodingDetector {

    public static String detectFileEncoding(String filePath) throws IOException {
        try (InputStream inputStream = new FileInputStream(filePath);
             BOMInputStream bomInputStream = new BOMInputStream(inputStream)) {

            String encoding = bomInputStream.getBOMCharsetName();
            if (encoding == null) {
                // 如果没有找到 BOM，则默认使用 UTF-8 编码
                encoding = "UTF-8";
            }

            return encoding;
        }
    }

    public static void main(String[] args) throws IOException {
        System.out.println( detectFileEncoding("D:\\project\\ieducation_training\\外部项目\\jundui_csv2sql\\data\\china\\population_data.csv") );
        System.out.println( detectFileEncoding("D:\\project\\ieducation_training\\外部项目\\jundui_csv2sql\\data\\china\\population_data.csv.sql") );
    }

}
